package com.yl.users.entity

import com.baomidou.mybatisplus.annotation.*
import com.yl.common.Flag
import com.yl.common.UserType
import com.yl.common.annotation.NoArg
import com.yl.common.getContext
import com.yl.users.common.NeedGerId
import com.yl.users.common.enum.UserPackageTypeEnum
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import kotlin.collections.HashSet

/**
 *
 * create by 2021/5/14.
 * @author udbjqr
 */
@NoArg
@TableName("company_user")
data class CompanyUser(

	@TableId(value = "id", type = IdType.AUTO)
	var id: Long,
	/**
	 * 登录名
	 */
	@TableField("login_name")
	var loginName: String? = null,
	/**
	 * 真实名称
	 */
	@TableField("real_name")
	var realName: String,
	/**
	 * 昵称
	 */
	@TableField(value = "nick_name", condition = SqlCondition.LIKE)
	var nickName: String? = null,

	/**
	 * 密码
	 */
	@TableField("password")
	var password: String? = null,

	/**
	 * 密码盐值
	 */
	@TableField("salt")
	var salt: String? = null,

	/**
	 * 头像路径
	 */
	@TableField("avatar_url")
	var avatarUrl: String? = null,

	/**
	 * 电话
	 */
	@TableField("phone")
	var phone: String? = null,

	/**
	 * 性别（1男，0女，2，未知）
	 */
	@TableField("gender")
	var gender: UserPackageTypeEnum= UserPackageTypeEnum.OTHER,


	/**
	 * 上级id
	 */
	@TableField("super_user_id")
	var superUserId: Long? = null,

	/**
	 * 创建时间
	 */
	@TableField("create_time")
	var createTime: Date? = null,

	/**
	 * 最后修改时间
	 */
	@TableField("update_time")
	var updateTime: Date? = null,

	/**
	 * 数据有效性
	 */
	@TableField("enabled")
	var enabled: Int = 1,

	@TableField("user_code")
	var userCode: String? = null,

	/**
	 * 医院id
	 */
	@TableField("company_id")
	var companyId: Long = getContext().companyId,


//	@TableField("user_type")
//	var userType: UserType = UserType.Unknown,
//    @TableField("flag")
//	var flag: Flag = Flag.UNKNOWN,


	@TableField(exist = false)
	var superior: CompanyUser? = null,

	@TableField(exist = false)
	var roleIds: List<Long>? = null,


	@TableField(exist = false)
	var fieldCodes: HashSet<String>? = null,

	/**
	 * 角色id
	 */
	@TableField(exist = false)
	var roleIdsStr: String? = null,

	/**
	 * 关联的角色
	 */
	@TableField(exist = false)
	var roles: List<CompanyRole>? = null,

	/**
	 * 标识
	 */
	@TableField("flag")
	var flag: Flag = Flag.UNKNOWN,

	/***
	 * 用户类型
	 */
	@TableField("user_type")
	var userType: UserType = UserType.Unknown,

	/**
	 * 最后登录的分院id
	 */
	@TableField("last_login_dept_id")
	var lastLoginDeptId: Long? = null,

	/**
	 * 医生职业类型 （医生，护士）
	 */
	@TableField("user_work_type")
	var userWorkType: UserPackageTypeEnum = UserPackageTypeEnum.COMPANY_USER_TYPE_DOCTOR,


	/**
	 * 部门id
	 */
	@TableField(exist = false)
	var deptId:Long ?= null,

	/**
	 * 部门名称
	 */
	@TableField(exist = false)
	var deptName:String ?= null,

	/**
	 * 分院id
	 */
	@TableField(exist = false)
	var subCompanyId:Long ?= null
) {
	@TableField(exist = false)
	private var _deptPerms: ConcurrentHashMap<Long, HashSet<String>>? = null
	val deptPerms: ConcurrentHashMap<Long, HashSet<String>>
		get() {
			if (_deptPerms == null) {
				_deptPerms = ConcurrentHashMap()
			}

			return _deptPerms!!
		}

	/**
	 * 判断当前用户是否是匿名用户
	 */
	val isAnonymous: Boolean get() = id == 0L

	/**
	 * 生成随机盐
	 */
	fun randomSalt() {
		salt = (1000..9999).random().toString()
	}


	val canBindRole:Boolean get() {
		return getContext().subCompanyId == subCompanyId
	}


}
